package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.regexp.internal.RE;
import entity.*;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.*;
import vo.BuyCargoodsVo;
import vo.GoodsPropertiesNameVo;
import vo.ParseBuycarVo;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xuweijie on 2017/5/19.
 */
public class SubmitOrderAction extends ActionSupport {

    @Autowired
    private CroAddressService croAddressService;
    @Autowired
    private CroMyqponService croMyqponService;
    @Autowired
    private CroPurchaseService croPurchaseService;
    @Autowired
    private CroBuycarsService croBuycarsService;
    @Autowired
    private CroFeeService croFeeService;

    private String numprice;
    private String purchaseid;
    private List<CroAddress> croAddresses;
    private List<CroMyqpon> croMyqpons;
    private List<BuyCargoodsVo> goodsdetails=new ArrayList<>();
    private double exfee;
    private String addressid;
    private String myqponid;
    private String paytype;
    private double totalprice;
    private String successResult;
    private String notes;


    /*
    * 提交订单页面（在这完善订单信息）
    * */
    public String submitorderlist(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer croCustomer= (CroCustomer) context.getAttribute("customer");
        croAddresses=croAddressService.listAddressByCustomer(croCustomer);
        List<CroMyqpon> myQpons = croMyqponService.FindUseMyqponByCusId(croCustomer.getId());
        croMyqpons = new ArrayList<>();
        for(CroMyqpon myqpon : myQpons){
            if(myqpon.getCroCoupons().getUplimit()<=Double.parseDouble(numprice)){
                croMyqpons.add(myqpon);
            }
        }
        CroPurchase croPurchase=croPurchaseService.findById(purchaseid);
        List<CroBuycars> buycars=croBuycarsService.listByPurchase(croPurchase);
        for(CroBuycars croBuycars:buycars){
            BuyCargoodsVo buyCargoodsVo=new BuyCargoodsVo();
            ParseBuycarVo parseBuycarVo= JSON.parseObject(croBuycars.getGoodsdetail(),ParseBuycarVo.class);
            buyCargoodsVo.setId(croBuycars.getId());
            buyCargoodsVo.setName(parseBuycarVo.getTitle());
            StringBuffer sb=new StringBuffer();
            for(GoodsPropertiesNameVo goodsdetail:parseBuycarVo.getPropertiesName()){
                sb.append(goodsdetail.getPropName()+":"+goodsdetail.getPropValueName()+",  ");
            }
            buyCargoodsVo.setDescrib(sb.toString());
            buyCargoodsVo.setPhoto(parseBuycarVo.getPicUrl());
            buyCargoodsVo.setPrice(parseBuycarVo.getPrice());
            buyCargoodsVo.setActprice(croBuycars.getActualprice());
            buyCargoodsVo.setNumbers(croBuycars.getNumbers());
            buyCargoodsVo.setSumprice(croBuycars.getSumprice());
            goodsdetails.add(buyCargoodsVo);
        }
        exfee=croFeeService.Findkuaidi().getCost();
        return SUCCESS;
    }

    /*
    * 提交订单
    * */
    public String payorder(){
        CroPurchase purchase=croPurchaseService.findById(purchaseid);
        CroAddress caddress=croAddressService.findAddressById(addressid);
        purchase.setCroAddress(caddress);
        purchase.setExcessfee(exfee);
        purchase.setTotalprice(totalprice);
        purchase.setPaytype(paytype);
        purchase.setOrderdate(new Date());
        purchase.setOrder_flag("1");
        purchase.setNotes(notes);
        if(!myqponid.equals("")){
            CroMyqpon pmyqpon=croMyqponService.FindMyqpon(myqponid).get(0);
            pmyqpon.setUsedate(new Date());
            pmyqpon.setStatus("1");
            croMyqponService.updateMyqponByEntity(pmyqpon);
        }
       if(croPurchaseService.updatePurchase(purchase)){
            successResult="0";
       }else {
           successResult="1";
       }
        return SUCCESS;
    }


    public String getNumprice() {
        return numprice;
    }

    public void setNumprice(String numprice) {
        this.numprice = numprice;
    }

    public String getPurchaseid() {
        return purchaseid;
    }

    public void setPurchaseid(String purchaseid) {
        this.purchaseid = purchaseid;
    }

    public List<CroAddress> getCroAddresses() {
        return croAddresses;
    }

    public void setCroAddresses(List<CroAddress> croAddresses) {
        this.croAddresses = croAddresses;
    }

    public List<CroMyqpon> getCroMyqpons() {
        return croMyqpons;
    }

    public void setCroMyqpons(List<CroMyqpon> croMyqpons) {
        this.croMyqpons = croMyqpons;
    }

    public List<BuyCargoodsVo> getGoodsdetails() {
        return goodsdetails;
    }

    public void setGoodsdetails(List<BuyCargoodsVo> goodsdetails) {
        this.goodsdetails = goodsdetails;
    }

    public double getExfee() {
        return exfee;
    }

    public void setExfee(double exfee) {
        this.exfee = exfee;
    }

    public String getAddressid() {
        return addressid;
    }

    public void setAddressid(String addressid) {
        this.addressid = addressid;
    }

    public String getMyqponid() {
        return myqponid;
    }

    public void setMyqponid(String myqponid) {
        this.myqponid = myqponid;
    }

    public String getPaytype() {
        return paytype;
    }

    public void setPaytype(String paytype) {
        this.paytype = paytype;
    }

    public double getTotalprice() {
        return totalprice;
    }

    public void setTotalprice(double totalprice) {
        this.totalprice = totalprice;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }
}
